package leetcode.editor.cn;

import java.util.Arrays;

public class P396RotateFunction {
    public static void main(String[] args) {
        Solution solution = new P396RotateFunction().new Solution();
        System.out.println(solution.maxRotateFunction(new int[]{4, 3, 2, 6}));

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maxRotateFunction1(int[] nums) {
            int len = nums.length;
            int max = Integer.MIN_VALUE;
            for (int start = 0; start < len; start++) {
                int total = 0;
                for (int i = start, j = 0; j < len; j++, i++) {
                    total += nums[i] * j;
                    i = i == len - 1 ? i - len : i;
                }
                max = Math.max(max, total);
            }
            return max;
        }
        public int maxRotateFunction(int[] nums) {
            int f = 0, n = nums.length, numSum = Arrays.stream(nums).sum();
            for (int i = 0; i < n; i++) {
                f += i * nums[i];
            }
            int res = f;
            for (int i = n - 1; i > 0; i--) {
                f += numSum - n * nums[i];
                res = Math.max(res, f);
            }
            return res;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}